package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.MemberService;
import com.itheima.service.SetMealService;
import com.itheima.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportController {
    @Reference
    private MemberService memberService;
    @Reference
    private SetMealService setMealService;
    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        try {
            //创建一个Map容器
            Map<String,Object> map = new HashMap<>();
            //创建一个List集合,用来存放日期集合
            List<String> months = new ArrayList<>();
            Calendar calendar = Calendar.getInstance();//获得日历对象
            //9,10,11,12,1,2,3,4,5,6,7,8
            calendar.add(Calendar.MONTH,-12);//向前推12个月
            //将所有月份进行遍历
            for (int i = 0; i < 12; i++) {
                calendar.add(Calendar.MONTH,1);//往后数1个月
                Date date = calendar.getTime();
                months.add(new SimpleDateFormat("yyyy.MM").format(date));
            }
            map.put("months",months);
            //查找每个月份的会员人数
            List<Integer> memberCount = memberService.findMemberCountByMonths(months);
            map.put("memberCount",memberCount);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }
    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
        try {
            //创建Map容器
            Map<String,Object> map = new HashMap<>();
            //创建setmealNames容器
            List<String> setmealNames = new ArrayList<>();
            //从数据库拿到setmealCount
            List<Map<String,Object>> setmealCount = setMealService.findSetmealCount();
            map.put("setmealCount",setmealCount);
            //遍历集合，拿到所有的套餐名，放入setmealNames容器中
            for (Map<String, Object> stringIntegerMap : setmealCount) {
                String name = (String) stringIntegerMap.get("name");
                setmealNames.add(name);
            }
            map.put("setmealNames",setmealNames);
            map.put("setmealCount",setmealCount);
            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }


}
